package cn.kgc.crowd1048.fundingadmin.controller;

import cn.kgc.crowd1048.fundingadmin.entity.TRole;
import cn.kgc.crowd1048.fundingadmin.service.TRoleService;
import cn.kgc.crowd1048.fundingadmin.util.ResultEntity;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    TRoleService tRoleService;


//    跳转到首页
    @RequestMapping("/to/page.html")
    public String toPage(){
        return "role-page";
    }
//查询的方法
    @ResponseBody
    @RequestMapping("/search/by/keyword")
    public ResultEntity<PageInfo<TRole>> search(
            @RequestParam(value="pageNum", defaultValue="1",required = false) Integer pageNum,
            @RequestParam(value="pageSize", defaultValue="2",required = false) Integer pageSize,
            @RequestParam(value="keyword", defaultValue="",required = false) String keyword
    ) {

        // 1.查询得到PageInfo对象
        PageInfo<TRole> pageInfo = tRoleService.queryForKeywordWithPage(pageNum, pageSize, keyword);

        // 2.封装结果对象返回
        return ResultEntity.successWithoutData(pageInfo);
    }

//回显模态框
    @ResponseBody
    @RequestMapping("/get/list/by/id/list")
    public ResultEntity<List<TRole>> getRoleListByIdList(@RequestBody List<Integer> roleIdList) {

        List<TRole> roleList = tRoleService.getRoleListByIdList(roleIdList);

        return ResultEntity.successWithoutData(roleList);
    }
//删除+多个id删除
    @ResponseBody
    @RequestMapping("/batch/remove")
    public ResultEntity<String> batchRemove(@RequestBody List<Integer> roleIdList) {

        tRoleService.batchRemove(roleIdList);

        return ResultEntity.successWithoutData();
    }

//    插入
    @ResponseBody
    @RequestMapping("save/role")
    public ResultEntity<String> saveRole(@RequestParam("roleName") String roleName) {

        tRoleService.saveRole(roleName);

        return ResultEntity.successWithoutData();
    }


//    修改的方法
@ResponseBody
@RequestMapping("/update/role")
public ResultEntity<String> updateRole(TRole role) {

    tRoleService.updateRole(role);

    return ResultEntity.successWithoutData();
}

}



